package com.fplugin.uverify;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.umeng.umverify.view.UMAuthUIConfig;

import java.util.Map;
import io.flutter.plugin.common.MethodChannel;

/** UverifyUI */
public class UverifyUI {
  private String TAG = "UVerifyUI=>TAG";
  
  private static Context context;
  MethodChannel methodChannel;
  private UverifyUtil util;

  UverifyUI(Context context, MethodChannel methodChannel) {
    this.context = context;
    this.methodChannel = methodChannel;
    this.util = new UverifyUtil(context, methodChannel);
  }

  public void setAuthUIBar(Map uiconfig,  UMAuthUIConfig.Builder builder){
    /// 授权页导航栏
    // int 设置状态栏颜色（系统版本 5.0 以上可设置）
    Object setStatusBarColor = util.getValueByMapKey(uiconfig, "setStatusBarColor");
    if(setStatusBarColor != null ) {
      builder.setStatusBarColor(Color.parseColor((String) setStatusBarColor));
    }
    // int 设置状态栏字体颜色（系统版本 6.0 以上可设置黑色、白色）。true 为黑色
    Object setLightColor = util.getValueByMapKey(uiconfig, "setLightColor");
    if(setLightColor != null) {
      builder.setLightColor((boolean) setLightColor);
    }
    // int 设置导航栏颜色
    Object setNavColor = util.getValueByMapKey(uiconfig, "setNavColor");

    if (setNavColor != null) {
      builder.setNavColor(Color.parseColor((String) setNavColor));
    }
    // String 设置导航栏标题文字
    Object setNavText = util.getValueByMapKey(uiconfig, "setNavText");
    if(setNavText != null) {
      builder.setNavText((String) setNavText);
    }
    // int 设置导航栏标题文字颜色
    Object getValueByMapKey = util.getValueByMapKey(uiconfig, "setNavTextColor");
    if(getValueByMapKey != null ) {
      builder.setNavTextColor((int) getValueByMapKey);
    }
    // int 设置导航栏标题文字大小
    Object setNavTextSize = util.getValueByMapKey(uiconfig, "setNavTextSize");
    if(setNavTextSize != null) {
      builder.setNavTextSize((int) setNavTextSize);
    }
    // String 设置导航栏返回键图片
    Object setNavReturnImgPath = util.getValueByMapKey(uiconfig, "setNavReturnImgPath");
    if(setNavReturnImgPath != null) {
      int res_id = util.getResourceByReflect((String) setNavReturnImgPath);
      if(res_id > 0) {
        builder.setNavReturnImgPath((String) setNavReturnImgPath);
      }
    }
    // boolean	设置导航栏返回按钮隐藏
    Object setNavReturnHidden = util.getValueByMapKey(uiconfig, "setNavReturnHidden");
    if(setNavReturnHidden != null) {
      builder.setNavReturnHidden((boolean) setNavReturnHidden);
    }
    // setNavHidden	boolean	设置默认导航栏是否隐藏
    Object setNavHidden = util.getValueByMapKey(uiconfig, "setNavHidden");
    if(setNavHidden != null) {
      builder.setNavHidden((boolean) util.getValueByMapKey(uiconfig, "setNavHidden"));
    }
    // setStatusBarHidden	boolean	设置状态栏是否隐藏
    Object setStatusBarHidden = util.getValueByMapKey(uiconfig, "setStatusBarHidden");
    if(setStatusBarHidden != null) {
      builder.setStatusBarHidden((boolean) setStatusBarHidden);
    }
    // int 设置状态栏UI属性 View.SYSTEM_UI_FLAG_LOW_PROFILE View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    Object setStatusBarUIFlag = util.getValueByMapKey(uiconfig, "setStatusBarUIFlag");
    if(setStatusBarUIFlag != null) {
      builder.setStatusBarUIFlag((int) setStatusBarUIFlag);
    }
    // int 设置协议页状态栏颜色（系统版本 5.0 以上可设置）不设置则与授权页设置一致
    Object setWebViewStatusBarColor = util.getValueByMapKey(uiconfig, "setWebViewStatusBarColor");
    if(setWebViewStatusBarColor != null) {
      builder.setWebViewStatusBarColor((int) Color.parseColor((String) setWebViewStatusBarColor));
    }
    // int 设置协议页顶部导航栏颜色 不设置则与授权页设置一致
    Object setWebNavColor = util.getValueByMapKey(uiconfig, "setWebNavColor");
    if(setWebNavColor != null){
      builder.setWebNavColor((int) Color.parseColor((String) setWebNavColor));
    }
    // int 设置协议页顶部导航栏标题颜色 不设置则与授权页设置一致
    Object setWebNavTextColor = util.getValueByMapKey(uiconfig, "setWebNavTextColor");
    if(setWebNavTextColor != null) {
      builder.setWebNavTextColor((int) Color.parseColor((String) setWebNavTextColor));
    }
    // int 设置协议页顶部导航栏文字大小 不设置则与授权页设置一致
    Object setWebNavTextSize = util.getValueByMapKey(uiconfig, "setWebNavTextSize");
    if(setWebNavTextSize != null) {
      builder.setWebNavTextSize((int) setWebNavTextSize);
    }
    // String 设置协议页导航栏返回按钮图片路径 不设置则与授权页设置一致
    //  Object webNavReturnImgPath = util.getValueByMapKey(uiconfig, "webNavReturnImgPath");
    //  if(webNavReturnImgPath != null) {
    //    int res_id = util.getResourceByReflect((String) webNavReturnImgPath);
    //    if(res_id > 0) {
    //      builder.webNavReturnImgPath((String) webNavReturnImgPath);
    //    }
    //  }
  }
  
  public void setAuthUILogan(Map uiconfig,  UMAuthUIConfig.Builder builder){
    /// 授权页Logo
    // boolean 隐藏logo
    Object setLogoHidden = util.getValueByMapKey(uiconfig, "setLogoHidden");
    if(setLogoHidden != null) {
      builder.setLogoHidden((boolean) setLogoHidden);
    }
    // String 设置logo 图片
    Object setLogoImgPath = util.getValueByMapKey(uiconfig, "setLogoImgPath");
    if(setLogoImgPath !=null) {
      int res_id = util.getResourceByReflect((String) setLogoImgPath);
      if (res_id > 0) {
        builder.setLogoImgPath((String) setLogoImgPath);
      }
    }
    // int 设置logo 控件宽度，单位dp
    Object setLogoWidth = util.getValueByMapKey(uiconfig, "setLogoWidth");
    if(setLogoWidth != null) {
      builder.setLogoWidth((int) setLogoWidth);
    }
    // int 设置logo 控件高度，单位dp
    Object setLogoHeight = util.getValueByMapKey(uiconfig, "setLogoHeight");
    if(setLogoHeight != null) {
      builder.setLogoHeight((int) setLogoHeight);
    }
    // int 设置logo 控件相对导航栏顶部的位移，单位dp
    Object setLogoOffsetY = util.getValueByMapKey(uiconfig, "setLogoOffsetY");
    if (setLogoOffsetY != null) {
      builder.setLogoOffsetY((int) setLogoOffsetY);
    }
    // int 设置logo 控件相对底部的位移，单位dp
    Object setLogoOffsetY_B = util.getValueByMapKey(uiconfig, "setLogoOffsetY_B");
    if(setLogoOffsetY_B != null) {
      builder.setLogoOffsetY_B((int) setLogoOffsetY_B);
    }

    // ImageView.ScaleType 设置logo图片缩放模式, MATRIX:0,FIT_XY: 1,FIT_START: 2,FIT_CENTER: 3,FIT_END: 4,CENTER: 5,CENTER_CROP: 6,CENTER_INSIDE: 7;
    Object setLogoScaleType = util.getValueByMapKey(uiconfig, "setLogoScaleType");
    if(setLogoScaleType != null) {
      builder.setLogoScaleType((ImageView.ScaleType) getScaleType((String) setLogoScaleType));
    }
  }
  
  public void setAuthUISlogan(Map uiconfig,  UMAuthUIConfig.Builder builder){

    /// 授权页Slogan
    // String 设置slogan 是否显示
    Object setSloganHidden = util.getValueByMapKey(uiconfig, "setSloganHidden");
    if (setSloganHidden != null) {
      builder.setSloganHidden((boolean) setSloganHidden);
    }
    // String 设置slogan 文字内容
    Object setSloganText = util.getValueByMapKey(uiconfig, "setSloganText");
    if (setSloganText != null) {
      builder.setSloganText((String) setSloganText);
    }

    // int 设置slogan 文字颜色
    Object setSloganTextColor = util.getValueByMapKey(uiconfig, "setSloganTextColor");
    if(setSloganTextColor != null) {
      builder.setSloganTextColor(Color.parseColor((String) setSloganTextColor));
    }
    // int 设置slogan 文字大小
    Object setSloganTextSize = util.getValueByMapKey(uiconfig, "setSloganTextSize");
    if(setSloganTextSize != null) {
      builder.setSloganTextSize((int) setSloganTextSize);
    }
    // int 设置slogan 相对导航栏顶部的位移，单位dp
    Object setSloganOffsetY = util.getValueByMapKey(uiconfig, "setSloganOffsetY");
    if(setSloganOffsetY != null) {
      builder.setSloganOffsetY((int) setSloganOffsetY);
    }
    // int 设置slogan 相对底部的位移，单位dp
    Object setSloganOffsetY_B = util.getValueByMapKey(uiconfig, "setSloganOffsetY_B");
    if(setSloganOffsetY_B != null) {
      builder.setSloganOffsetY_B((int) setSloganOffsetY_B);
    }
  }
  
  public void setAuthUIPhone(Map uiconfig,  UMAuthUIConfig.Builder builder){
    /// 授权页号码栏
    // int	设置手机号码字体颜色
    Object setNumberColor = util.getValueByMapKey(uiconfig, "setNumberColor");
    if (setNumberColor != null) {
      builder.setNumberColor((int) Color.parseColor((String) setNumberColor));
    }
    // int	设置手机号码字体大小
    Object setNumberSize = util.getValueByMapKey(uiconfig, "setNumberSize");
    if (setNumberSize != null) {
      builder.setNumberSize((int) setNumberSize);
    }
    // int	设置号码栏控件相对导航栏顶部的位移，单位 dp
    Object setNumFieldOffsetY = util.getValueByMapKey(uiconfig, "setNumFieldOffsetY");
    if (setNumFieldOffsetY != null) {
      builder.setNumFieldOffsetY((int) setNumFieldOffsetY);
    }
    // int	设置号码栏控件相对底部的位移，单位 dp
    Object setNumFieldOffsetY_B = util.getValueByMapKey(uiconfig, "setNumFieldOffsetY_B");
    if (setNumFieldOffsetY_B != null) {
      builder.setNumFieldOffsetY_B((int) setNumFieldOffsetY_B);
    }
    // int	设置号码栏相对于默认位置的X 轴偏移量，单位dp
    Object setNumberFieldOffsetX = util.getValueByMapKey(uiconfig, "setNumberFieldOffsetX");
    if (setNumberFieldOffsetX != null) {
      builder.setNumberFieldOffsetX((int) setNumberFieldOffsetX);
    }
    // int	设置手机号掩码的布局对齐方式，只支持Gravity.CENTER_HORIZONTAL、Gravity.LEFT、Gravity.RIGHT三种对齐方式
    Object setNumberLayoutGravity = util.getValueByMapKey(uiconfig, "setNumberLayoutGravity");
    if (setNumberLayoutGravity != null) {
      builder.setNumberLayoutGravity((int) getGravityType((String) setNumberLayoutGravity));
    }
  }
  
  public void setAuthUILoginButton(Map uiconfig,  UMAuthUIConfig.Builder builder){
    /// 授权页登录按钮
    // String 设置登录按钮文字
    Object setLogBtnText = util.getValueByMapKey(uiconfig, "setLogBtnText");
    if (setLogBtnText != null) {
      builder.setLogBtnText((String) setLogBtnText);
    }
    // int	设置登录按钮文字颜色
    Object setLogBtnTextColor = util.getValueByMapKey(uiconfig, "setLogBtnTextColor");
    if (setLogBtnTextColor != null) {
      builder.setLogBtnTextColor((int) Color.parseColor((String) setLogBtnTextColor));
    }
    // int	设置登录按钮文字大小
    Object setLogBtnTextSize = util.getValueByMapKey(uiconfig, "setLogBtnTextSize");
    if (setLogBtnTextSize != null) {
      builder.setLogBtnTextSize((int) setLogBtnTextSize);
    }
    // int	设置登录按钮宽度，单位 dp
    Object setLogBtnWidth = util.getValueByMapKey(uiconfig, "setLogBtnWidth");
    if (setLogBtnWidth != null) {
      builder.setLogBtnWidth((int) setLogBtnWidth);
    }
    // int	设置登录按钮高度，单位dp
    Object setLogBtnHeight = util.getValueByMapKey(uiconfig, "setLogBtnHeight");
    if (setLogBtnHeight != null) {
      builder.setLogBtnHeight((int) setLogBtnHeight);
    }
    // int	设置登录按钮相对于屏幕左右边缘边距
    Object setLogBtnMarginLeftAndRight = util.getValueByMapKey(uiconfig, "setLogBtnMarginLeftAndRight");
    if (setLogBtnMarginLeftAndRight != null) {
      builder.setLogBtnMarginLeftAndRight((int) setLogBtnMarginLeftAndRight);
    }
    // String	设置登录按钮背景图片路径
    Object setLogBtnBackgroundPath = util.getValueByMapKey(uiconfig, "setLogBtnBackgroundPath");
    if (setLogBtnBackgroundPath != null) {
      int res_id = util.getResourceByReflect((String) setLogBtnBackgroundPath);
      if (res_id > 0) {
        builder.setLogBtnBackgroundPath((String) setLogBtnBackgroundPath);
      }
    }
    // int	设置登录按钮相对导航栏顶部的位移，单位 dp
    Object setLogBtnOffsetY = util.getValueByMapKey(uiconfig, "setLogBtnOffsetY");
    if (setLogBtnOffsetY !=null) {
      builder.setLogBtnOffsetY((int) setLogBtnOffsetY);
    }
    // int	设置登录按钮相对底部的位移，单位 dp
    Object setLogBtnOffsetY_B = util.getValueByMapKey(uiconfig, "setLogBtnOffsetY_B");
    if (setLogBtnOffsetY_B != null) {
      builder.setLogBtnOffsetY_B((int) setLogBtnOffsetY_B);
    }
    // String	设置登录loading dialog 背景图片路径
    Object setLoadingImgPath = util.getValueByMapKey(uiconfig, "setLoadingImgPath");
    if (setLoadingImgPath != null) {
      int res_id = util.getResourceByReflect((String) setLoadingImgPath);
      if (res_id > 0) {
        builder.setLoadingImgPath((String) setLoadingImgPath);
      }
    }
    // int	设置登陆按钮X轴偏移量，如果设置了setLogBtnMarginLeftAndRight，
    // 并且布局对齐方式为左对齐或者右对齐,则会在margin的基础上再增加offsetX的偏移量，如果是居中对齐，则仅仅会在居中的基础上再做offsetX的偏移。
    Object setLogBtnOffsetX = util.getValueByMapKey(uiconfig, "setLogBtnOffsetX");
    if (setLogBtnOffsetX != null) {
      builder.setLogBtnOffsetX((int) setLogBtnOffsetX);
    }
    // int	设置登陆按钮布局对齐方式，只支持Gravity.CENTER_HORIZONTAL、Gravity.LEFT、Gravity.RIGHT三种对齐方式
    Object setLogBtnLayoutGravity = util.getValueByMapKey(uiconfig, "setLogBtnLayoutGravity");
    if (setLogBtnLayoutGravity != null) {
      builder.setLogBtnLayoutGravity((int) getGravityType((String) setLogBtnLayoutGravity));
    }
  }
  
  public void setAuthUIPrivacy(Map uiconfig,  UMAuthUIConfig.Builder builder){
    /// 授权页隐私栏
    // String,String 设置开发者隐私条款 1 名称和URL(名称，url)
    Object setAppPrivacyOne = util.getValueByMapKey(uiconfig, "setAppPrivacyOne");
    if(setAppPrivacyOne != null) {
      String[] appPrivacyOne = util.splitStringBySeparator((String) setAppPrivacyOne, ",");
      builder.setAppPrivacyOne((String) appPrivacyOne[0], (String) appPrivacyOne[1]);
    }
    // String,String 设置开发者隐私条款 2 名称和URL(名称，url)
    Object setAppPrivacyTwo = util.getValueByMapKey(uiconfig, "setAppPrivacyTwo");
    if (setAppPrivacyTwo != null) {
      String[] appPrivacyTwo = util.splitStringBySeparator((String) setAppPrivacyTwo, ",");
      builder.setAppPrivacyTwo((String) appPrivacyTwo[0], (String) appPrivacyTwo[1]);
    }
    // int,int 设置隐私条款名称颜色(基础文字颜色，协议文字颜色)
    Object setAppPrivacyColor = util.getValueByMapKey(uiconfig, "setAppPrivacyColor");
    if(setAppPrivacyColor != null) {
      String[] setAppPrivacyColorArr = util.splitStringBySeparator((String) setAppPrivacyColor, ",");
      builder.setAppPrivacyColor((int) Color.parseColor(setAppPrivacyColorArr[0]), (int) Color.parseColor(setAppPrivacyColorArr[1]));
    }
    // int 设置隐私条款相对导航栏顶部的位移，单位dp
    Object setPrivacyOffsetY = util.getValueByMapKey(uiconfig, "setPrivacyOffsetY");
    if (setPrivacyOffsetY != null) {
      builder.setPrivacyOffsetY((int) setPrivacyOffsetY);
    }
    // int 设置隐私条款相对底部的位移，单位dp
    Object setPrivacyOffsetY_B = util.getValueByMapKey(uiconfig, "setPrivacyOffsetY_B");
    if (setPrivacyOffsetY_B != null) {
      builder.setPrivacyOffsetY_B((int) setPrivacyOffsetY_B);
    }
    // boolean 设置隐私条款是否默认勾选
    Object setPrivacyState = util.getValueByMapKey(uiconfig, "setPrivacyState");
    if (setPrivacyState != null) {
      builder.setPrivacyState((boolean) setPrivacyState);
    }
    // int 设置隐私条款文字对齐方式，单位Gravity.xxx
    Object setProtocolGravity = util.getValueByMapKey(uiconfig, "setProtocolGravity");
    if (setProtocolGravity != null) {
      builder.setProtocolGravity((int) getGravityType((String) setProtocolGravity));
    }
    // int 设置隐私条款文字大小，单位sp
    Object setPrivacyTextSize = util.getValueByMapKey(uiconfig, "setPrivacyTextSize");
    if (setPrivacyTextSize != null) {
      builder.setPrivacyTextSize((int) setPrivacyTextSize);
    }
    // int 设置隐私条款距离手机左右边缘的边距，单位dp
    Object setPrivacyMargin = util.getValueByMapKey(uiconfig, "setPrivacyMargin");
    if (setPrivacyMargin != null) {
      builder.setPrivacyMargin((int) setPrivacyMargin);
    }
    // String 设置开发者隐私条款前置自定义文案
    Object setPrivacyBefore = util.getValueByMapKey(uiconfig, "setPrivacyBefore");
    if (setPrivacyBefore != null) {
      builder.setPrivacyBefore((String) setPrivacyBefore);
    }
    // String 设置开发者隐私条款尾部自定义文案
    Object setPrivacyEnd = util.getValueByMapKey(uiconfig, "setPrivacyEnd");
    if (setPrivacyEnd != null) {
      builder.setPrivacyEnd((String) setPrivacyEnd);
    }
    // boolean 设置复选框是否隐藏
    Object setCheckboxHidden = util.getValueByMapKey(uiconfig, "setCheckboxHidden");
    if (setCheckboxHidden != null) {
      builder.setCheckboxHidden((boolean) setCheckboxHidden);
    }
    // String 设置复选框未选中时图片
    Object setUncheckedImgPath = util.getValueByMapKey(uiconfig, "setUncheckedImgPath");
    if (setUncheckedImgPath != null) {
      int res_id = util.getResourceByReflect((String) setUncheckedImgPath);
      if (res_id > 0) {
        builder.setUncheckedImgPath((String) setUncheckedImgPath);
      }
    }
    // String 设置复选框选中时图片
    Object setCheckedImgPath = util.getValueByMapKey(uiconfig, "setCheckedImgPath");
    if (setCheckedImgPath != null) {
      int res_id = util.getResourceByReflect((String) setCheckedImgPath);
      if (res_id > 0) {
        builder.setCheckedImgPath((String) setCheckedImgPath);
      }
    }
    // String 设置运营商协议前缀符号，只能设置一个字符，且只能设置<>()《》【】『』[]（）中的一个
    Object setVendorPrivacyPrefix = util.getValueByMapKey(uiconfig, "setVendorPrivacyPrefix");
    if (setVendorPrivacyPrefix != null) {
      builder.setVendorPrivacyPrefix((String) setVendorPrivacyPrefix);
    }
    // String 设置运营商协议后缀符号，只能设置一个字符，且只能设置<>()《》【】『』[]（）中的一个
    Object setVendorPrivacySuffix = util.getValueByMapKey(uiconfig, "setVendorPrivacySuffix");
    if (setVendorPrivacySuffix != null) {
      builder.setVendorPrivacySuffix((String) setVendorPrivacySuffix);
    }
    // int 设置隐私栏的布局对齐方式，该接口控制了整个隐私栏（包含checkbox）在其父布局中的对齐方式，
    // 而setProtocolGravity控制的是隐私协议文字内容在文本框中的对齐方式
    Object setProtocolLayoutGravity = util.getValueByMapKey(uiconfig, "setProtocolLayoutGravity");
    if (setProtocolLayoutGravity != null) {
      builder.setProtocolLayoutGravity((int) getGravityType((String) setProtocolLayoutGravity));
    }
    // int 设置隐私栏X轴偏移量，单位dp
    Object setPrivacyOffsetX = util.getValueByMapKey(uiconfig, "setPrivacyOffsetX");
    if (setPrivacyOffsetX != null) {
      builder.setPrivacyOffsetX((int) setPrivacyOffsetX);
    }
    // boolean 设置checkbox未勾选时，点击登录按钮toast是否显示
    Object setLogBtnToastHidden = util.getValueByMapKey(uiconfig, "setLogBtnToastHidden");
    if (setLogBtnToastHidden != null) {
      builder.setLogBtnToastHidden((boolean) setLogBtnToastHidden);
    }
  }
  
  public void setAuthUISwitch(Map uiconfig,  UMAuthUIConfig.Builder builder) {
    /// 切换方式控件
    // boolean 设置切换按钮点是否可见
    Object setSwitchAccHidden = util.getValueByMapKey(uiconfig, "setSwitchAccHidden");
    if (setSwitchAccHidden != null) {
      builder.setSwitchAccHidden((boolean) setSwitchAccHidden);
    }
    // String 设置切换按钮文字内容
    Object setSwitchAccText = util.getValueByMapKey(uiconfig, "setSwitchAccText");
    if (setSwitchAccText != null) {
      builder.setSwitchAccText((String) setSwitchAccText);
    }
    // int 设置切换按钮文字颜色
    Object setSwitchAccTextColor = util.getValueByMapKey(uiconfig, "setSwitchAccTextColor");
    if (setSwitchAccTextColor != null) {
      builder.setSwitchAccTextColor((int) Color.parseColor((String) setSwitchAccTextColor));
    }
    // int 设置切换按钮文字大小
    Object setSwitchAccTextSize = util.getValueByMapKey(uiconfig, "setSwitchAccTextSize");
    if (setSwitchAccTextSize != null) {
      builder.setSwitchAccTextSize((int) setSwitchAccTextSize);
    }
    // int 设置换按钮相对导航栏顶部的位移，单位 dp
    Object setSwitchOffsetY = util.getValueByMapKey(uiconfig, "setSwitchOffsetY");
    if (setSwitchOffsetY != null) {
      builder.setSwitchOffsetY((int) setSwitchOffsetY);
    }
    // int 设置换按钮相对底部的位移，单位 dp
    Object setSwitchOffsetY_B = util.getValueByMapKey(uiconfig, "setSwitchOffsetY_B");
    if (setSwitchOffsetY_B != null) {
      builder.setSwitchOffsetY_B((int) setSwitchOffsetY_B);
    }
  }
  
  public void setAuthUIPage(Map uiconfig,  UMAuthUIConfig.Builder builder) {
    /// 页面相关函数
    // String 设置授权页进场动画
    Object setAuthPageActIn = util.getValueByMapKey(uiconfig, "setAuthPageActIn");
    if (setAuthPageActIn != null) {
      String[] setAuthPageActInArr = util.splitStringBySeparator((String) setAuthPageActIn, ",");
      builder.setAuthPageActIn((String) setAuthPageActInArr[0], (String) setAuthPageActInArr[1]);
    }
    // String 设置授权页退出动画
    Object setAuthPageActOut = util.getValueByMapKey(uiconfig, "setAuthPageActOut");
    if (setAuthPageActOut != null) {
      String[] setAuthPageActOutArr = util.splitStringBySeparator((String) setAuthPageActOut, ",");
      builder.setAuthPageActOut((String) setAuthPageActOutArr[0], (String) setAuthPageActOutArr[1]);
    }
    // int 设置屏幕方向，取值为ActivityInfo中的屏幕方向常量值，比如 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    Object setScreenOrientation = util.getValueByMapKey(uiconfig, "setScreenOrientation");
    if (setScreenOrientation != null) {
      builder.setScreenOrientation((int) getActivityInfo((String)setScreenOrientation));
    }
    // String 设置授权页背景图drawable资源的目录，不需要加后缀，
    // 比如图片在drawable中的存放目录是res/drawable-xxhdpi/loading.png,则传入参数为”loading”，setPageBackgroundPath(“loading”)
    Object setPageBackgroundPath = util.getValueByMapKey(uiconfig, "setPageBackgroundPath");
    if(setPageBackgroundPath !=null) {
      int res_id = util.getResourceByReflect((String) setPageBackgroundPath);
      if (res_id > 0) {
        builder.setPageBackgroundPath((String) setPageBackgroundPath);
      }
    }
    // int 设置弹窗模式授权页宽度，单位dp,设置大于0即为弹窗模式
    Object setDialogWidth = util.getValueByMapKey(uiconfig, "setDialogWidth");
    if (setDialogWidth != null) {
      builder.setDialogWidth((int) setDialogWidth);
    }
    // int	设置弹窗模式授权页高度，单位dp，设置大于0即为弹窗模式
    Object setDialogHeight = util.getValueByMapKey(uiconfig, "setDialogHeight");
    if (setDialogHeight != null) {
      builder.setDialogHeight((int) setDialogHeight);
    }
    // int	设置弹窗模式授权页X轴偏移，单位dp
    Object setDialogOffsetX = util.getValueByMapKey(uiconfig, "setDialogOffsetX");
    if (setDialogOffsetX != null) {
      builder.setDialogOffsetX((int) setDialogOffsetX);
    }
    // int	设置弹窗模式授权页Y轴偏移,单位dp
    Object setDialogOffsetY = util.getValueByMapKey(uiconfig, "setDialogOffsetY");
    if (setDialogOffsetY != null) {
      builder.setDialogOffsetY((int) setDialogOffsetY);
    }
    // boolean 设置授权页是否居于底部
    Object setDialogBottom = util.getValueByMapKey(uiconfig, "setDialogBottom");
    if (setDialogBottom != null) {
      builder.setDialogBottom((boolean) setDialogBottom);
    }
  }

  static int getActivityInfo(String type) {
    switch (type) {
      case "LAUNCH_SINGLE_TOP":
        return ActivityInfo.LAUNCH_SINGLE_TOP;
      case "LAUNCH_SINGLE_TASK":
        return ActivityInfo.LAUNCH_SINGLE_TASK;
      case "LAUNCH_SINGLE_INSTANCE":
        return ActivityInfo.LAUNCH_SINGLE_INSTANCE;
      default:
        return ActivityInfo.LAUNCH_MULTIPLE;
    }
  }
  
  static int getGravityType(String type) {
    int value = 0;
    switch(type) {
      // case "AXIS_SPECIFIED":
      //   value =  Gravity.AXIS_SPECIFIED;
      // case "AXIS_PULL_BEFORE":
      //   value =  Gravity.AXIS_PULL_BEFORE;
      // case "AXIS_PULL_AFTER":
      //   value =  Gravity.AXIS_PULL_AFTER;
      // case "AXIS_CLIP":
      //   value =  Gravity.AXIS_CLIP;
      // case "AXIS_X_SHIFT":
      //   value =  Gravity.AXIS_X_SHIFT;
      // case "AXIS_Y_SHIFT":
      //   value =  Gravity.AXIS_Y_SHIFT;
      case "TOP":
        value =  Gravity.TOP;
      case "BOTTOM":
        value =  Gravity.BOTTOM;
      case "LEFT":
        value =  Gravity.LEFT;
      case "RIGHT":
        value =  Gravity.RIGHT;
      // case "CENTER_VERTICAL":
      //   value =  Gravity.CENTER_VERTICAL;
      // case "FILL_VERTICAL":
      //   value =  Gravity.FILL_VERTICAL;
      // case "CENTER_HORIZONTAL":
      //   value =  Gravity.CENTER_HORIZONTAL;
      // case "FILL_HORIZONTAL":
      //   value =  Gravity.FILL_HORIZONTAL;
      case "CENTER":
        value =  Gravity.CENTER;
      // case "FILL":
      //   value =  Gravity.FILL;
      // case "CLIP_VERTICAL":
      //   value =  Gravity.CLIP_VERTICAL;
      // case "CLIP_HORIZONTAL":
      //   value =  Gravity.CLIP_HORIZONTAL;
      // case "RELATIVE_LAYOUT_DIRECTION":
      //   value =  Gravity.RELATIVE_LAYOUT_DIRECTION;
      // case "HORIZONTAL_GRAVITY_MASK":
      //   value =  Gravity.HORIZONTAL_GRAVITY_MASK;
      // case "VERTICAL_GRAVITY_MASK":
      //   value =  Gravity.VERTICAL_GRAVITY_MASK;
      // case "DISPLAY_CLIP_VERTICAL":
      //   value =  Gravity.DISPLAY_CLIP_VERTICAL;
      // case "DISPLAY_CLIP_HORIZONTAL":
      //   value =  Gravity.DISPLAY_CLIP_HORIZONTAL;
      // case "START":
      //   value =  Gravity.START;
      // case "END":
      //   value =  Gravity.END;
      // case "RELATIVE_HORIZONTAL_GRAVITY_MASK":
      //   value =  Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
      default:
        value =  Gravity.NO_GRAVITY;
    }
    return value;
  }
  
  static int getRelativeLayout(String type) {
    switch(type) {
      case "LEFT":
        return RelativeLayout.ALIGN_LEFT;
      case "RIGHT":
        return RelativeLayout.ALIGN_RIGHT;
      case "CENTER_HORIZONTAL":
        return RelativeLayout.CENTER_HORIZONTAL;
      case "BOTTOM":
        return RelativeLayout.ALIGN_BOTTOM;
      default:
        return 0;
    }
  }

  static ImageView.ScaleType getScaleType(String type) {
    // MATRIX:0,FIT_XY: 1,FIT_START: 2,FIT_CENTER: 3,FIT_END: 4,CENTER: 5,CENTER_CROP: 6,CENTER_INSIDE: 7
    switch (type) {
      case "FIT_XY":
        return ImageView.ScaleType.FIT_XY;
      case "FIT_START":
        return ImageView.ScaleType.FIT_START;
      case "FIT_CENTER":
        return ImageView.ScaleType.FIT_CENTER;
      case "FIT_END":
        return ImageView.ScaleType.FIT_END;
      case "CENTER":
        return ImageView.ScaleType.CENTER;
      case "CENTER_CROP":
        return ImageView.ScaleType.CENTER_CROP;
      case "CENTER_INSIDE":
        return ImageView.ScaleType.CENTER_INSIDE;
      default :
        return ImageView.ScaleType.MATRIX;
    }
  }

  /**
   * @funtion addCustomTextWidget 添加自定义TextView
   * @param params
   * @param builder
   */
  @RequiresApi(api = Build.VERSION_CODES.P)
  public TextView addCustomTextViewWidget(Map params, UMAuthUIConfig.Builder builder) {
    Log.d(TAG, "addCustomTextView:params=" + params);
    TextView customView = new TextView(context);
    return setCommonCustomWidget(customView, params);
  }

  /**
   * @function addCustomButtonWidget 添加自定义添加ButtonWidget
   * @param params {Map}
   * @param builder {UMAuthUIConfig.Builder}
   */
  @RequiresApi(api = Build.VERSION_CODES.P)
  public TextView addCustomButtonWidget(Map params, UMAuthUIConfig.Builder builder) {
    Log.d(TAG, "addCustomButtonWidgets:params=" + params);
    Button customWidget = new Button(context);
    // 设置背景图（只支持button设置）
    String btnNormalImageName = (String) params.get("btnNormalImageName");
    String btnPressedImageName = (String) params.get("btnPressedImageName");
    if (btnPressedImageName == null) {
      btnPressedImageName = btnNormalImageName;
    }
    if(btnNormalImageName != null) {
      setButtonSelector(customWidget, btnNormalImageName, btnPressedImageName);
    }
    return setCommonCustomWidget(customWidget, params);
  }

  /**
   * @function setButtonSelector 设置button背景图片点击效果
   * @param button          按钮
   * @param normalImageName 常态下背景图
   * @param pressImageName  点击时背景图
   */
  public void setButtonSelector(Button button, String normalImageName, String pressImageName) {
    Log.d(TAG, "setButtonSelector normalImageName=" + normalImageName + "，pressImageName=" + pressImageName);

    StateListDrawable drawable = new StateListDrawable();
    Resources res = context.getResources();

    final int normal_resId = util.getResourceByReflect(normalImageName);
    final int select_resId = util.getResourceByReflect(pressImageName);

    Bitmap normal_bmp = BitmapFactory.decodeResource(res, normal_resId);
    Drawable normal_drawable = new BitmapDrawable(res, normal_bmp);

    Bitmap select_bmp = BitmapFactory.decodeResource(res, select_resId);
    Drawable select_drawable = new BitmapDrawable(res, select_bmp);

    // 未选中
    drawable.addState(new int[]{-android.R.attr.state_pressed}, normal_drawable);
    // 选中
    drawable.addState(new int[]{android.R.attr.state_pressed}, select_drawable);

    button.setBackground(drawable);
  }

  /**
   * @funtion setCommonCustomWidget 设置公共自定义weiget
   * @param customWidget
   * @param params
   */
  @RequiresApi(api = Build.VERSION_CODES.P)
  public TextView setCommonCustomWidget(TextView customWidget, Map params) {
    // 设置text
    final String text = (String) params.get("text");
    customWidget.setText(text);

    // 设置字体颜色
    Object color = params.get("color");
    if (color != null) {
        customWidget.setTextColor((int) Color.parseColor((String)color));
    }

    // 设置字体大小
    Object fontSize = params.get("fontSize");
    if (fontSize != null) {
      int _fontSize = (int) fontSize;
      if (_fontSize > 0) {
        customWidget.setTextSize((float) _fontSize);
      }
    }

    // 设置背景颜色
    Object backgroundColor = params.get("backgroundColor");
    if (backgroundColor != null) {
      customWidget.setBackgroundColor((int) Color.parseColor((String) backgroundColor));
    }

    // 设置背景图通过资源设置
    Object backgroundResource = params.get("backgroundResource");
    if (backgroundResource != null) {
      int res_id = util.getResourceByReflect((String) backgroundResource);
      if (res_id > 0) {
        customWidget.setBackgroundResource(res_id);
      }
    }

    // 下划线
    Object isShowUnderline = params.get("isShowUnderline");
    if (isShowUnderline != null) {
      boolean isflag = (boolean)isShowUnderline;
      if(isflag) {
        customWidget.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); // 下划线
        customWidget.getPaint().setAntiAlias(true); // 抗锯齿
      }
    }

    // 设置对齐方式
    Object textAlignment = params.get("textAlignment");
    if (textAlignment != null) {
      int gravity = getGravityType((String) textAlignment);
      customWidget.setGravity(gravity);
    }

    Object lineHeight = params.get("lineHeight");
    if (lineHeight != null) {
      int _lineHeight = (int) lineHeight;
      customWidget.setLineHeight((int) util.dp2Pix(context, (float) _lineHeight));
    }



    Object lines = params.get("lines");
    if(lines != null) {
      customWidget.setLines((int) lines); // 设置行数
    }

    Object isSingleLine =  params.get("isSingleLine");
    if (isSingleLine != null) {
      // 设置是否单行显示，多余的就 ...
      customWidget.setSingleLine((boolean) isSingleLine);
    }

    // margin 位置
    Object left = params.get("left");
    Object top = params.get("top");
    Object width = params.get("width");
    Object height = params.get("height");

    int layoutParamsWith = -2; // RelativeLayout.LayoutParams.WRAP_CONTENT
    if (width != null) {
      int _width = (int) width;
      if (_width < 0) {
        layoutParamsWith = (int) _width;
      }
    }
    int layoutParamsHeight = -2; // RelativeLayout.LayoutParams.WRAP_CONTENT
    if (height != null) {
      int _height = (int) height;
      if (_height < 0) {
        layoutParamsHeight = (int) _height;
      }
    }
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layoutParamsWith, layoutParamsHeight);

    if (left != null) {
      int _left = (int) left;
      if (_left > 0) {
        layoutParams.leftMargin = util.dp2Pix(context, (float) _left);
      }
    }
    if (top != null) {
      int _top = (int) top;
      if (_top > 0) {
        layoutParams.topMargin = util.dp2Pix(context, (float) _top);
      }
    }
    if (width != null) {
      int _width = (int) width;
      if (_width > 0) {
        layoutParams.width = util.dp2Pix(context, (float) _width);
      }
    }
    if (height != null) {
      int _height = (int) height;
      if (_height > 0) {
        layoutParams.height = util.dp2Pix(context, (float) _height);
      }
    }

    Object layoutGravity = params.get("layoutGravity");
    if(layoutGravity != null) {
      layoutParams.addRule(getRelativeLayout((String) layoutGravity));
    }


    customWidget.setLayoutParams(layoutParams);

    return customWidget;
  }
}

